package org.lsda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lsda.sched.LsdaScheduler;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * Servlet implementation class JobList
 */
public class ListJob extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(ListJob.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListJob() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ArrayList<JobDetail> jobs = null;
		ArrayList<Integer> statuses = new ArrayList<Integer>();
		int jtPageSize = Integer.parseInt(request.getParameter("jtPageSize"));
		int jtStartIndex =Integer.parseInt(request.getParameter("jtStartIndex"));
		String jtSorting = request.getParameter("jtSorting");

		List<? extends Trigger> triggers = null;
		JsonObject json = new JsonObject();
		json.addProperty("Result", "OK");
		try {
			jobs = LsdaScheduler.listJobs();
			for (JobDetail jd : jobs) {
				triggers = LsdaScheduler.getTriggers(jd.getKey().getGroup(), jd
						.getKey().getName());
				if (triggers == null || triggers.size() == 0)
					statuses.add(0);
				else
					statuses.add(1);
			}
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", e.getMessage());
			log.fatal(e.getCause());
			PrintWriter writer = response.getWriter();
//			System.out.println(json.toString());
			writer.write(json.toString());
			return;
		}
		int total = 0;
		// TODO Auto-generated method stub

		JsonArray records = new JsonArray();
		if (jobs != null) {
			for (int i = 0; i < jobs.size(); i++) {
				// for(String v:jd.getJobDataMap().getKeys())
				// System.out.println(v);
				JobDetail jd = jobs.get(i);
				int status = statuses.get(i);
				JsonObject record = new JsonObject();
				record.addProperty("JobName",
						jd.getJobDataMap().getString("jobId"));
				record.addProperty("JobGroup",
						jd.getJobDataMap().getString("groupId"));
				record.addProperty("JobDesc",
						jd.getJobDataMap().getString("description"));
				record.addProperty("UpdateTime",
						jd.getJobDataMap().getString("updateTime"));
				record.addProperty("JobStatus", status);
				records.add(record);
				total++;
			}

		}

		json.add("Records", records);
		json.addProperty("TotalRecordCount", total);
		PrintWriter writer = response.getWriter();
//		System.out.println(json.toString());
		writer.write(json.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
